home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / WANDR401.ZIP / sources / SAVE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-24  |  4.3 KB  |  174 lines

  1. /*          Save.c                           */
  2.  
  3. /*   Functions for the saving game           */
  4.  
  5.  
  6.  
  7. #include "wand_head.h"
  8. #include <errno.h>
  9.  
  10. extern char lscreen[NOOFROWS][ROWLEN+1];
  11. extern int saved_game;
  12. extern char screen_name[61];
  13. extern void crypt_file();
  14. extern int *messagewin;
  15.  
  16. struct saved_game {
  17.     short num;
  18.     long  score;
  19.     short bell;
  20.     short maxmoves;
  21.     short num_monsters;
  22. };
  23.  
  24. struct save_vars zz;
  25.  
  26. extern struct mon_rec start_of_list, *tail_of_list;
  27.  
  28. void save_game(num, score, bell, maxmoves)
  29. int  num, *bell, maxmoves;
  30. long *score;
  31. {
  32.     char fname[128], buf[70], *fp;
  33.     FILE *fo;
  34.     struct saved_game s;
  35.     extern char *getenv();
  36.     struct mon_rec *mp;
  37.  
  38.     if ((char *)NULL == (fp = getenv("SAVENAME"))) {
  39.         save_game_file_name(fname);
  40.     fp = fname;
  41.     }
  42.     if ((FILE *)NULL == (fo = fopen(fp, W_BIN))) {
  43.     perror(fp);
  44.     return;
  45.     }
  46.  
  47.     s.num = num;
  48.     s.score = *score;
  49.     s.bell = *bell;
  50.     s.maxmoves = maxmoves;
  51.     s.num_monsters = 0;
  52.  
  53.     mp = &start_of_list;        /* first entry is dummy    */
  54.     while (mp != tail_of_list) {
  55.     mp = mp->next;
  56.     s.num_monsters++;    /* count them monsters    */
  57.     }
  58.  
  59.     if ((1 != fwrite((char *)&s, sizeof(s), 1, fo)) ||
  60.     (1 != fwrite((char *)lscreen, sizeof(lscreen), 1, fo)) ||
  61.     (1 != fwrite((char *)&zz, sizeof(zz), 1, fo))) {
  62.     sprintf(buf,"Write error on '%s'\n", fname);
  63.         alert_message(buf);
  64.     fclose(fo);
  65.     return;
  66.     }
  67.  
  68.     mp = &start_of_list;
  69.     while (mp != tail_of_list) {
  70.     /* save them monsters    */
  71.     mp = mp->next;
  72.     if (1 != fwrite((char *)mp, sizeof(struct mon_rec), 1, fo)) {
  73.         sprintf(buf,"Write error on '%s'\n", fname);
  74.             alert_message(buf);
  75.         fclose(fo);
  76.         return;
  77.     }
  78.     }
  79.     fwrite(screen_name,sizeof(char),strlen(screen_name),fo);
  80.     fclose(fo);
  81.    notify_message("Game saved.");
  82.    return;
  83. }
  84.  
  85. int restore_game(num, score, bell, maxmoves)
  86. int  *num, *bell, *maxmoves;
  87. long *score;
  88. {
  89.     FILE *fi;
  90.     struct saved_game s;
  91.     struct mon_rec *mp, *tmp, tmp_monst;
  92.     char fname[128], *fp;
  93.     extern char *getenv();
  94.     char buffer[80];
  95.  
  96.     if ((char *)NULL == (fp = getenv("SAVENAME"))) {
  97.         restore_game_file_name(fname);
  98.     fp = fname;
  99.     }
  100.     fi = fopen(fp, R_BIN);
  101.     if (fi == NULL) 
  102.         {
  103.     sprintf(buffer,"Cannot find file %s", fp);
  104.         alert_message(buffer);
  105.         return -1;
  106.         }
  107.         
  108.     else if ( (1 != fread((char *)&s, sizeof(s), 1, fi)) ||
  109.     (1 != fread((char *)lscreen, sizeof(lscreen), 1, fi)) ||
  110.     (1 != fread((char *)&zz, sizeof(zz), 1, fi)) ) {
  111.     sprintf(buffer,"Read error on %s", fp);
  112.         alert_message(buffer);
  113.     fclose(fi);
  114. //    exit(1);
  115.         return -1;
  116.     }
  117.  
  118.     *num = s.num;
  119.     *score = s.score;
  120.     *bell = s.bell;
  121.     *maxmoves = s.maxmoves;
  122.  
  123.     /* free any monsters already on chain, to start clean */
  124.     mp = start_of_list.next;
  125.     while ((mp != NULL) && (mp != &start_of_list)) {
  126.     /* free them monsters    */
  127.     tmp = mp;
  128.     mp = mp->next;
  129.     free(tmp);
  130.     }
  131.  
  132.     /* re-initialize the monster list    */
  133.     /* start_of_list = {0,0,0,0,0,NULL,NULL}; */
  134.     start_of_list.x = 0;
  135.     start_of_list.y = 0;
  136.     start_of_list.mx = 0;
  137.     start_of_list.my = 0;
  138.     start_of_list.under = 0;
  139.     start_of_list.next = (struct mon_rec *)NULL;
  140.     start_of_list.prev = (struct mon_rec *)NULL;
  141.  
  142.     tail_of_list = &start_of_list;
  143.  
  144.     while (s.num_monsters--) {
  145.     /* use make_monster to allocate the monster structures    */
  146.     /* to get all the linking right without even trying    */
  147.     if ((struct mon_rec *)NULL == (mp = make_monster(0, 0))) {
  148.             alert_message("Monster alloc error");
  149.         //printf("Monster alloc error on '%s'n", fp);
  150.         fclose(fi);
  151.         //exit(1);
  152.     }
  153.     if (1 != fread((char *)&tmp_monst, sizeof(struct mon_rec), 1, fi)) {
  154.         //endwin();
  155.             alert_message("Monster read error");
  156.         //printf("Monster read error on '%s'\n", fp);
  157.         //printf("Cannot restore game --- sorry.\n");
  158.         fclose(fi);
  159.       //  exit(1);
  160.     }
  161.     /* copy info without trashing links    */
  162.     mp->x     = tmp_monst.x;
  163.     mp->y     = tmp_monst.y;
  164.     mp->mx    = tmp_monst.mx;
  165.     mp->my    = tmp_monst.my;
  166.     mp->under = tmp_monst.under;
  167.     }
  168.     if (fgets(screen_name,61,fi) == NULL)
  169.     *screen_name = '#';
  170.     fclose(fi);
  171.     saved_game = 1;
  172.     return  0;
  173. }
  174.